<?php
	
	class Locations{
		private $db;
		
		public function __construct(){
			$this->db =& Application::getInstance()->database('default');
		}
		
		/**
		 * 
		 * @param string $keyword
		 * @return array id, name, city, branch
		 */
		public function searchCompany($keyword){
			$stmt = $this->db->query('SELECT id, name, city, branch FROM companies WHERE keywords LIKE ? LIMIT 7', array('%' . $keyword . '%'));
			
			if($stmt['rowCount'] > 0){
				return $stmt['result'];
			}
			
			return null;
		}	
		
		/**
		 *
		 * @param string $name
		 * @param string $countryCode iso country code for example: "nl"
		 * @return array id, name, country
		 */
		public function readCityFromCountry($name, $countryCode){
			$stmt = $this->db->query('SELECT id, name, country FROM cities WHERE name = ? AND country = ?', array($name, $countryCode));
			
			if($stmt['rowCount'] > 0){
				return $stmt['result'];
			}
			
			return null;
		}
		
		/**
		 *
		 * @param int $id
		 * @return array id, name
		 */
		public function readCompany($id){
			$stmt = $this->db->query('SELECT id, name, city, country, branch FROM companies WHERE id = ?', array($id));
			
			if($stmt['rowCount'] > 0){
				return $stmt['result'][0];
			}
			
			return null;
		}
		
		/**
		 *
		 * @param int $id
		 * @return array id, name, country
		 */
		public function readCity($id){
			$stmt = $this->db->query('SELECT id, name, country FROM cities WHERE id = ?', array($id));
			
			if($stmt['rowCount'] > 0){
				return $stmt['result'][0];
			}
			
			return null;
		}
	}

//	class Locations{
//		private $db;
//		
//		public function __construct(){
//			$this->db =& Application::getInstance()->database('default');
//		}
//		
//		public function searchCompany($keyword){
//			$stmt = $this->db->query('SELECT `id`, `name`, `city`, `branch` FROM `companies` WHERE `keywords` LIKE ? LIMIT 7', array('%' . $keyword . '%'));
//			
//			if($stmt['rowCount'] > 0){
//				return $stmt['result'];
//			}
//			
//			return null;
//		}
//		
//		public function companyExists($id){
//			$stmt = $this->db->query('SELECT `id` FROM `companies` WHERE `id` = ?', array($id));
//			
//			if($stmt['rowCount'] > 0){
//				return true;
//			}
//			
//			return false;
//		}
//		
//		public function getCountry($id){
//			$stmt = $this->db->query('SELECT * FROM `countries` WHERE `id` = ?', array($id));
//			
//			if($stmt['rowCount'] > 0){
//				return $stmt['result'][0];
//			}
//			
//			return null;
//		}
//		
//		public function getCity($id){
//			$stmt = $this->db->query('SELECT * FROM `cities` WHERE `id` = ?', array($id));
//			
//			if($stmt['rowCount'] > 0){
//				return $stmt['result'][0];
//			}
//			
//			return null;
//		}
//		
//		public function getCompany($id){
//			$stmt = $this->db->query('SELECT `name`, `city`, `branch` FROM `companies` WHERE `id` = ?', array($id));
//			
//			if($stmt['rowCount'] > 0){
//				return $stmt['result'][0];
//			}
//			
//			return null;
//		}
//				
//		public function userCreateLocation($name, $userId, $cityName){
//			$this->db->query('INSERT INTO `companies`(`name`, `city`, `createdByUser`, `createdOn`) VALUES (?, ?, ?, NOW())', array(
//				$name,
//				$cityName,
//				$userId
//			));
//		}
//		
//		public function searchCityInCountry($keyword, $countryCode){
//			$stmt = $this->db->query('SELECT * FROM `cities` WHERE `name` = ? AND `country` = ?', array($keyword, $countryCode));
//			
//			if($stmt['rowCount'] > 0){
//				return $stmt['result'];
//			}
//			
//			return null;
//		}
//		
//		public function searchCity($keyword){
//			$stmt = $this->db->query('SELECT * FROM `cities` WHERE `name` LIKE ?', array('%' . $keyword . '%'));
//			
//			if($stmt['rowCount'] > 0){
//				return $stmt['result'];
//			}
//			
//			return null;
//		}
//		
//		public function searchCountry($keyword){
//			$stmt = $this->db->query('SELECT * FROM `countries` WHERE `name` LIKE ?', array('%' . $keyword . '%'));
//			
//			if($stmt['rowCount'] > 0){
//				return $stmt['result'];
//			}
//			
//			return null;
//		}
//		
//		public function searchCompanyInCity($keyword, $place){
//			$stmt = $this->db->query('SELECT * FROM `companies` WHERE `name` LIKE ? AND `city` LIKE ?', array('%' . $keyword . '%', '%' . $place . '%'));
//			
//			if($stmt['rowCount'] > 0){
//				return $stmt['result'];
//			}
//			
//			return null;
//		}
//	}

// File end.